/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.task;

import java.util.ArrayList;
import java.util.List;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.activity.intent.TaskIntentHolder;
import lt.marsrutai.mobile.android.exception.DataAccessException;
import lt.marsrutai.mobile.android.model.Stop;
import lt.marsrutai.mobile.android.task.GetStopsTask.Criteria;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class GetStopsTask extends NotifyingAsyncTask<Criteria, Integer, List<Stop>> {

    public static final String BROADCAST_ACTION = "lt.marsrutai.mobile.android.task.GetStopsTask";
    
    private static final String TAG = "GetStopsAsyncTask";
    
    @Override
    protected List<Stop> doInBackground(Criteria... params) {        
        List<Stop> aggregatedStops = new ArrayList<Stop>();
        for (Criteria criteria : params) {
            aggregatedStops.addAll(getStops(criteria));
        }
        
        return aggregatedStops;
    }

    @Override
    protected Intent getResultIntent(List<Stop> result) {
        TaskIntentHolder intentHolder = new TaskIntentHolder(super.getResultIntent(result));
        intentHolder.setResult(result, Stop.class);
        return intentHolder.getIntent();
    }

    @Override
    protected String getBroadcastAction() {
        return BROADCAST_ACTION;
    }
    
    private List<Stop> getStops(Criteria intentHolder) {
        String scopeFolder = intentHolder.getScopeFolder();
        String trackId = intentHolder.getTrackId();
        String stopId = intentHolder.getStopId();
        
        List<Stop> stops = new ArrayList<Stop>();
        
        try {
            if (null == trackId) {
                stops.add(AppContext.getDataService().getStopById(scopeFolder, stopId));
            }
            else {
                stops = AppContext.getDataService().getTrackStops(scopeFolder, trackId);
            }
        }
        catch (DataAccessException e) {
            Log.e(TAG, e.getMessage());
            setResultStatus(RESULT_NOT_RESOLVED);
        }
        
        return stops;
    }

    public static class Criteria implements Parcelable {

        public static final Parcelable.Creator<Criteria> CREATOR = new Parcelable.Creator<Criteria>() {

            @Override
            public Criteria createFromParcel(Parcel source) {
                return new Criteria(source);
            }

            @Override
            public Criteria[] newArray(int size) {
                return new Criteria[size];
            }
        };

        private String scopeFolder;
        private String trackId;
        private String stopId;

        public Criteria() {
        }

        public Criteria(String scopeFolder, String trackId, String stopId) {
            this.scopeFolder = scopeFolder;
            this.trackId = trackId;
            this.stopId = stopId;
        }

        public Criteria(Parcel in) {
            scopeFolder = in.readString();
            trackId = in.readString();
            stopId = in.readString();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(scopeFolder);
            dest.writeString(trackId);
            dest.writeString(stopId);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public String getScopeFolder() {
            return scopeFolder;
        }

        public void setScopeFolder(String scopeFolder) {
            this.scopeFolder = scopeFolder;
        }

        public String getTrackId() {
            return trackId;
        }

        public void setTrackId(String trackId) {
            this.trackId = trackId;
        }

        public String getStopId() {
            return stopId;
        }

        public void setStopId(String stopId) {
            this.stopId = stopId;
        }
    }
}
